#include <iostream>
#include <stdlib.h>

int candidate1;
int candidate2;
int candidate3;

void Find(int *ID, int N)
{
    int nTimes1 = 0 ;
    int nTimes2 = 0 ;
    int nTimes3 = 0 ;
    int i;

    for( i = 0; i < N; i++) {
        if (nTimes1 == 0) {
            candidate1 = ID[i], nTimes1 = 1;
        } else {
            if (candidate1 == ID[i]) {
                nTimes1++;
            } else if (nTimes2 == 0) {
                candidate2 = ID[i], nTimes2 = 1;
            } else {
                if (candidate2 == ID[i]) {
                    nTimes2++;
                } else {
                    if (nTimes3 == 0) {
                        candidate3 = ID[i], nTimes3 = 1;
                    } else if (candidate3 == ID[i]) {
                        nTimes3++;
                    } else {
                        nTimes1--;
                        nTimes2--;
                        nTimes3--;
                    }
                }
            }
        }
    }
}

int main()
{
    int a[] = {0, 4, 1, 4, 0, 4, 1, 4, 1, 0, 3, 3, 0, 3, 3, 3};
    Find(a, 16);
    std::cout << candidate1 << " " << candidate2 << " " << candidate3 << std::endl;
    system("pause");
    return 0;
}

